home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / gencodec / source / writecatalog / writecatalogmain.c < prev   
C/C++ Source or Header  |  1999-01-01  |  10KB  |  419 lines

  1. /* Main-Header File inserted by GenCodeC */
  2. /* Libraries */
  3. #include <libraries/mui.h>
  4. #include <libraries/gadtools.h> /* for BARLABEL in MenuItem */
  5.  
  6. /* Prototypes */
  7. #ifdef __GNUC__
  8. #include <proto/muimaster.h>
  9. #include <proto/exec.h>
  10. #include <proto/alib.h>
  11. #include <proto/dos.h>
  12. #else
  13. #include <clib/muimaster_protos.h>
  14. #include <clib/exec_protos.h>
  15. #include <clib/alib_protos.h>
  16. #include <clib/dos_protos.h>
  17. #include <pragmas/muimaster_pragmas.h>
  18. #endif /* __GNUC__ */
  19.  
  20. /*  Ansi  */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24. /* Increase stack size */
  25. LONG __stack=8192;
  26. /* GenCodeC header end */
  27.  
  28. /* Include generated by GenCodeC */
  29. #include "WriteCatalogGUI.h"
  30.  
  31. #include <clib/asl_protos.h>
  32. #include <string.h>
  33.  
  34. #include "Tools.h"
  35. #include "TextField.h"
  36. #include "WriteCatalogFiles.h"
  37.  
  38. struct ObjTextField     *TF_Catalog = NULL;
  39. BOOL                    NormalEnd;
  40.  
  41. /* Declarations for libraries (inserted by GenCodeC) */
  42. struct Library * MUIMasterBase;
  43.  
  44. struct Library * IntuitionBase = NULL;
  45. struct Library * TextFieldBase = NULL;
  46.  
  47. /* Init() function */
  48. void init( void )
  49. {
  50.     if (!(IntuitionBase = OpenLibrary("intuition.library",36)))
  51.     {
  52.         exit(20);
  53.     }
  54.  
  55.     if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
  56.     {
  57.         DisplayMsg("Can't Open MUIMaster Library\n");
  58.         CloseLibrary(IntuitionBase);
  59.         exit(20);
  60.     }
  61.  
  62.     if (!(TextFieldBase = OpenLibrary("gadgets/textfield.gadget", 0)))
  63.     {
  64.         DisplayMsg("Can't Open Textfield gadget\n");
  65.         CloseLibrary(MUIMasterBase);
  66.         CloseLibrary(IntuitionBase);
  67.         exit(20);
  68.     }
  69. }
  70. /* GenCodeC init() end */
  71.  
  72. /* End() function */
  73. void end(void *App )
  74. {
  75.     struct ObjApp * app = App;
  76.  
  77.     if (app)
  78.     {
  79.         set(app->WI_WriteCatalog,
  80.             MUIA_Window_Open,FALSE);
  81.  
  82.         if (TF_Catalog && TF_Catalog->textfield)
  83.             DoMethod(app->GR_Text, OM_REMOVE, TF_Catalog->textfield);
  84.  
  85.         DisposeTextField(TF_Catalog);
  86.  
  87.         DisposeApp((struct ObjApp *)App);
  88.     }
  89.  
  90.     CloseAllFiles(!NormalEnd);
  91.     ClearMemory(!NormalEnd);
  92.  
  93.     CloseLibrary(TextFieldBase);
  94.     CloseLibrary(MUIMasterBase);
  95.     CloseLibrary(IntuitionBase);
  96.     exit(20);
  97. }
  98. /* GenCodeC end() end */
  99.  
  100. /* Get a Catalog Description File Name with a File Requester */
  101. BOOL GetFile(STRPTR *FileName)
  102. {
  103.     struct Library            *AslBase;
  104.     struct FileRequester    *FileRequest;
  105.  
  106.     if (!(AslBase = OpenLibrary(AslName,0)))
  107.     {
  108.         DisplayMsg("Can't Open Asl Library\n");
  109.         return FALSE;
  110.     }
  111.  
  112.     if (!(FileRequest=AllocAslRequestTags(ASL_FileRequest,
  113.                                           ASLFR_TitleText,"Choose a file",
  114.                                           ASLFR_InitialDrawer,"PROGDIR:",
  115.                                           TAG_DONE)))
  116.     {
  117.         DisplayMsg("Can't alloc a FileRequester \n");
  118.         CloseLibrary(AslBase);
  119.         return FALSE;
  120.     }
  121.  
  122.     if (AslRequest(FileRequest,NULL) &&
  123.         strcmp(FileRequest->fr_File,""))
  124.     {
  125.         if (strlen(FileRequest->fr_File)>=3 &&
  126.             FileRequest->fr_File[strlen(FileRequest->fr_File)-3]=='.' &&
  127.             FileRequest->fr_File[strlen(FileRequest->fr_File)-2]=='c' &&
  128.             FileRequest->fr_File[strlen(FileRequest->fr_File)-1]=='d')
  129.         {
  130.             if (!(*FileName=AllocMemory(strlen(FileRequest->fr_Drawer)+1+
  131.                                         strlen(FileRequest->fr_File)+1,FALSE)))
  132.             {
  133.                 FreeAslRequest(FileRequest);
  134.                 CloseLibrary(AslBase);
  135.                 return FALSE;
  136.             }
  137.             strcpy(*FileName,FileRequest->fr_Drawer);
  138.             AddPart(*FileName,FileRequest->fr_File,strlen(FileRequest->fr_Drawer)+1+
  139.                                                    strlen(FileRequest->fr_File)+1);
  140.             FreeAslRequest(FileRequest);
  141.             CloseLibrary(AslBase);
  142.             return TRUE;
  143.         }
  144.         else
  145.         {
  146.             char *msg;
  147.  
  148.             if (!(msg=AllocMemory(strlen(FileRequest->fr_File)+38+1,FALSE)))
  149.             {
  150.                 FreeAslRequest(FileRequest);
  151.                 CloseLibrary(AslBase);
  152.                 return FALSE;
  153.             }
  154.             sprintf(msg,"%s is not a Catalog Description File !!\n",FileRequest->fr_File);
  155.             DisplayMsg(msg);
  156.             FreeMemory(msg);
  157.             FreeAslRequest(FileRequest);
  158.             CloseLibrary(AslBase);
  159.             return FALSE;
  160.         }
  161.     }
  162.     else
  163.     {
  164.         FreeAslRequest(FileRequest);
  165.         CloseLibrary(AslBase);
  166.         return FALSE;
  167.     }
  168. }
  169.  
  170. /* Main Function inserted by GenCodeC */
  171. int main(int argc,char **argv)
  172. {
  173.     struct ObjApp             *App = NULL;    /* Object */
  174.     BOOL                    running = TRUE;
  175.     ULONG                    signal;
  176.     LONG                    Args[3]={0L,0L,0L};
  177.     struct RDArgs             *result;
  178.     char                    *CatalogName;
  179.     char                    *GetString = NULL;
  180.     char                    *CatalogText;
  181.     BOOL                    NoGenerate=FALSE;
  182.  
  183.     /* Program initialisation : generated by GenCodeC */
  184.     init();
  185.  
  186.     NormalEnd = TRUE;
  187.     if (!(result=ReadArgs("CDN=CatalogDescriptionName/K,GSN=GetStringName/K,Reserved/S",Args,NULL)))
  188.     {
  189.         printf("Error\n");
  190.         printf("Usage : WriteCatalog CDN=CatalogDescriptionName/A/K GSN=GetStringName/A/K\n");
  191.         end(App);
  192.     }
  193.  
  194.     /* test reserved argument */
  195.     NoGenerate=((Args[2]!=0) ? TRUE : FALSE);
  196.  
  197.     /* test CatalogDescriptionName */
  198.     if (Args[0]==0)
  199.     {
  200.         if (!GetFile((STRPTR *)&CatalogName))
  201.         {
  202.             FreeArgs(result);
  203.             end(App);
  204.         }
  205.     }
  206.     else
  207.     {
  208.         if (!(CatalogName=AllocMemory(strlen((char *)Args[0])+1,FALSE)))
  209.         {
  210.             FreeArgs(result);
  211.             end(App);
  212.         }
  213.         strcpy(CatalogName,(char *)Args[0]);
  214.  
  215.         if (strlen(CatalogName)<3 ||
  216.             CatalogName[strlen(CatalogName)-3]!='.' ||
  217.             CatalogName[strlen(CatalogName)-2]!='c' ||
  218.             CatalogName[strlen(CatalogName)-1]!='d')
  219.         {
  220.             char *msg;
  221.  
  222.             if (!(msg=AllocMemory(strlen(CatalogName)+38+1,FALSE)))
  223.             {
  224.                 FreeMemory(CatalogName);
  225.                 FreeArgs(result);
  226.                 end(App);
  227.             }
  228.             sprintf(msg,"%s is not a Catalog Description File !!\n",CatalogName);
  229.             DisplayMsg(msg);
  230.             FreeMemory(msg);
  231.             FreeMemory(CatalogName);
  232.             FreeArgs(result);
  233.             end(App);
  234.         }
  235.     }
  236.  
  237.     /* test GetString Name */
  238.     if (Args[1]!=0)
  239.     {
  240.         if (!(GetString=AllocMemory(strlen((char *)Args[1])+1,FALSE)))
  241.         {
  242.             FreeMemory(CatalogName);
  243.             FreeArgs(result);
  244.             end(App);
  245.         }
  246.         strcpy(GetString,(char *)Args[1]);
  247.     }
  248.  
  249.     FreeArgs(result);
  250.  
  251.     NormalEnd = FALSE;
  252.     /* Create Object : generated by GenCodeC */
  253.     if (!(App = CreateApp()))
  254.     {
  255.         DisplayMsg("Can't Create App\n");
  256.         end(NULL);
  257.     }
  258.  
  259.     SetDataQuit((void *)App);
  260.     SetFunctionQuit(end);
  261.  
  262.     /* Create and fill a textfield object */
  263.     if (!(TF_Catalog = CreateTextField()))
  264.     {
  265.         DisplayMsg("Can't Create Textfield object\n");
  266.         end((void *)App);
  267.     }
  268.     DoMethod(App->GR_Text, OM_ADDMEMBER, TF_Catalog->textfield);
  269.     CatalogText = LoadFileInRAM(CatalogName,FALSE);
  270.     if (CatalogText)
  271.     {
  272.         set(TF_Catalog->text,TEXTFIELD_Text,CatalogText);
  273.         FreeMemory(CatalogText);
  274.     }
  275.  
  276.     set(App->STR_GetStringName,MUIA_String_Contents,GetString);
  277.  
  278.     if (NoGenerate)
  279.     {
  280.         set(App->BT_GenerateFiles,MUIA_ShowMe,FALSE);
  281.         set(App->GR_GetStringName,MUIA_ShowMe,FALSE);
  282.     }
  283.  
  284.     set(App->WI_WriteCatalog,
  285.         MUIA_Window_Open,TRUE);
  286.  
  287.     while (running)
  288.     {
  289.         switch (DoMethod(App->App,MUIM_Application_Input,&signal))
  290.         {
  291.             case MUIV_Application_ReturnID_Quit:
  292.                 running = FALSE;
  293.                 break;
  294.  
  295.             /* Insert your code between the "case" statement and comment "end of case ..." */
  296.             case ID_BT_GenerateFiles:
  297.             {
  298.                 ULONG size;
  299.                 char  *text;
  300.                 FILE  *file;
  301.                 char  *Catalog_h_File;
  302.                 char  *Catalog_c_File;
  303.                 char  *GetStringName;
  304.  
  305.                 get(App->STR_GetStringName,MUIA_String_Contents,&GetStringName);
  306.                 if (strlen(GetStringName)==0)
  307.                 {
  308.                     DisplayMsg("Please enter a string in the field \"GetString Name\"");
  309.                 }
  310.                 else
  311.                 {
  312.                     if (GetString==0)
  313.                     {
  314.                         GetString=AllocMemory(strlen(GetStringName)+1,TRUE);
  315.                         strcpy(GetString,GetStringName);
  316.                     }
  317.  
  318.                     set(TF_Catalog->text,TEXTFIELD_ReadOnly,TRUE);
  319.                     get(TF_Catalog->text,TEXTFIELD_Size,&size);
  320.                     get(TF_Catalog->text,TEXTFIELD_Text,&text);
  321.  
  322.                     if (!(file=fopenFile(CatalogName,"w",FALSE)))
  323.                     {
  324.                         char *msg;
  325.  
  326.                         msg=AllocMemory(14+strlen(CatalogName)+1,TRUE);
  327.                         sprintf(msg,"Can't update %s\n",CatalogName);
  328.                         DisplayMsg(msg);
  329.                         FreeMemory(msg);
  330.                         FreeMemory(CatalogName);
  331.                         FreeMemory(GetString);
  332.                         end((void *)App);
  333.                     }
  334.                     fwrite(text,size,1,file);
  335.                     fcloseFile(file);
  336.  
  337.                     remove_extend(CatalogName);
  338.  
  339.                     Catalog_h_File = AllocMemory(strlen(CatalogName)+7,TRUE);
  340.                     strcpy(Catalog_h_File, CatalogName);
  341.                     strcat(Catalog_h_File, "_cat");
  342.                     add_extend(Catalog_h_File, ".h");
  343.  
  344.                     Catalog_c_File = AllocMemory(strlen(CatalogName)+7,TRUE);
  345.                     strcpy(Catalog_c_File, CatalogName);
  346.                     strcat(Catalog_c_File, "_cat");
  347.                     add_extend(Catalog_c_File, ".c");
  348.  
  349.                     add_extend(CatalogName,".cd");
  350.  
  351.                     if (!Write_Catalog_h_File(Catalog_h_File,CatalogName,GetString))
  352.                     {
  353.                         FreeMemory(CatalogName);
  354.                         FreeMemory(GetString);
  355.                         FreeMemory(Catalog_h_File);
  356.                         FreeMemory(Catalog_c_File);
  357.                         end((void *)App);
  358.                     }
  359.  
  360.                     if (!Write_Catalog_c_File(Catalog_c_File,CatalogName,GetString))
  361.                     {
  362.                         FreeMemory(CatalogName);
  363.                         FreeMemory(GetString);
  364.                         FreeMemory(Catalog_h_File);
  365.                         FreeMemory(Catalog_c_File);
  366.                         end((void *)App);
  367.                     }
  368.  
  369.                     FreeMemory(Catalog_h_File);
  370.                     FreeMemory(Catalog_c_File);
  371.  
  372.                     set(TF_Catalog->text,TEXTFIELD_ReadOnly,FALSE);
  373.                 }
  374.             }
  375.             break;
  376.             /* end of case ID_BT_GenerateFiles */
  377.  
  378.             case ID_BT_Save:
  379.             {
  380.                 ULONG size;
  381.                 char  *text;
  382.                 FILE  *file;
  383.  
  384.                 set(TF_Catalog->text,TEXTFIELD_ReadOnly,TRUE);
  385.                 get(TF_Catalog->text,TEXTFIELD_Size,&size);
  386.                 get(TF_Catalog->text,TEXTFIELD_Text,&text);
  387.  
  388.                 if (!(file=fopenFile(CatalogName,"w",FALSE)))
  389.                 {
  390.                     char *msg;
  391.  
  392.                     msg=AllocMemory(14+strlen(CatalogName)+1,TRUE);
  393.                     sprintf(msg,"Can't update %s\n",CatalogName);
  394.                     DisplayMsg(msg);
  395.                     FreeMemory(msg);
  396.                     FreeMemory(CatalogName);
  397.                     FreeMemory(GetString);
  398.                     end((void *)App);
  399.                 }
  400.                 fwrite(text,size,1,file);
  401.                 fcloseFile(file);
  402.  
  403.                 set(TF_Catalog->text,TEXTFIELD_ReadOnly,FALSE);
  404.             }
  405.             break;
  406.             /* end of case ID_BT_Save */
  407.  
  408.             /* End computing of IDCMP */
  409.         }
  410.         if (running && signal) Wait(signal);
  411.     }
  412.  
  413.     FreeMemory(CatalogName);
  414.     FreeMemory(GetString);
  415.  
  416.     NormalEnd = TRUE;
  417.     end((void *)App);
  418. }
  419.